package action.member;

import java.sql.DriverManager;
import java.sql.ResultSet;

import org.apache.commons.lang.StringUtils;
import org.apache.struts2.ServletActionContext;

import security.MySecurityContextHandle;
import service.interf.IDeviceInfo;
import action.common.MyBaseAction;
import action.login.AdminAction;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
import com.opensymphony.xwork2.ModelDriven;
import common.impl.exception.ServiceException;

import dao.pojo.DeviceInfo;

public class DeviceInfoBrowAction extends MyBaseAction<DeviceInfo> implements
		ModelDriven<DeviceInfo> {

	private static final long serialVersionUID = 1L;
	private IDeviceInfo iDeviceInfo;
	private int deviceStatus;

	public void setiDeviceInfo(IDeviceInfo iDeviceInfo) {
		this.iDeviceInfo = iDeviceInfo;
	}

	public void setDeviceStatus(int deviceStatus) {
		this.deviceStatus = deviceStatus;
	}

	public DeviceInfoBrowAction() {
		super(DeviceInfo.class);
	}

	public DeviceInfo getModel() {
		return model;
	}

	/**
	 * 获取列表
	 */
	public void getlist() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iDeviceInfo.getList(userId, deviceStatus, model
					.getDeviceModel(), model.getDeviceSn(), model.getPage(),
					model.getRows());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 获取单个
	 */
	public void getbyid() {
		String r = "1";
		try {
			r = iDeviceInfo.findById("DeviceInfo", "*", "json", "deviceIndex",
					model.getDeviceIndex());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 重启
	 */
	public void rebootbydevicesn() {
		String r = "1";
		try {
			r = iDeviceInfo.rebootByDeviceSn(model.getDeviceSn());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 升级
	 */
	public void update() {
		String r = "1";
		try {
			r = iDeviceInfo.updateAndReboot(model.getIds(), model
					.getDeviceVersion(), false);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 升级并重启
	 */
	public void updateandreboot() {
		String r = "1";
		try {
			r = iDeviceInfo.updateAndReboot(model.getIds(), model
					.getDeviceVersion(), true);
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 远程
	 */
	public void remote() {
		String r = "1";
		try {
			r = iDeviceInfo.remote(model.getDeviceIndex());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 删除
	 */
	public void del() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iDeviceInfo.del(userId, model.getIds());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 修改备注
	 */
	public void editnote() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			r = iDeviceInfo.editNote(userId, model.getDeviceIndex(), model
					.getNote());
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

	/**
	 * 获取真实sn
	 */
	public void getsnbymap() {
		String r = "1";
		try {
			Long userId = MySecurityContextHandle.getUserId();
			if (null == userId)
				throw new ServiceException("请先登录");
			r = AdminAction.snMap.get(model.getDeviceSn());
			if (StringUtils.isEmpty(r))
				r = model.getDeviceSn();
		} catch (Exception e) {
			r = errorHandle.handleErr(e);
		}
		servletHandle.writeToClient1(ServletActionContext.getResponse(), r);
	}

}
